package home.study.xxy.bs.service.impl;

import home.study.xxy.bs.entity.LoginUser;
import home.study.xxy.bs.entity.Page;
import home.study.xxy.bs.entity.ResultList;
import home.study.xxy.bs.entity.ResultObj;
import home.study.xxy.bs.entity.User;
import home.study.xxy.bs.mapper.LoginMapper;
import home.study.xxy.bs.service.LoginService;
import home.study.xxy.bs.utils.JWTUtil;
import home.study.xxy.bs.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: LoginServiceImpl
 * @功能:
 * @Author Xiao Xiangyu
 * @Date: 2021/4/11 19:34
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private AuthenticationManager manager;

//    @Autowired
//    private RedisCache redisCache;

    @Autowired
    private RedisTemplate redisTemplate;

    public int checkLogin(User user){
        String pwdMd5  = MD5Utils.stringToMD5(user.getPwd());

        return loginMapper.loginCheck(user.getUserName(),pwdMd5);
    }

    @Override
    public ResultObj getLoginData(User user) {
        //2022.05.27  添加spring'security 登录验证

        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPwd());

        //使用spring security 认证
        Authentication authenticate = manager.authenticate(authentication);
        if (authenticate == null){
            throw new RuntimeException("登录失败!");
        }


        //生成token
        LoginUser authorities = (LoginUser) authenticate.getPrincipal();
        String id = authorities.getUser().getId();

        String token = JWTUtil.createToken(id);

        authorities.setToken(token);

//        HashMap<String, String> result = new HashMap<>();
//        String tokenUser = JWTUtil.createToken(token);
//        result.put("token", token);


        //保存用户信息到redis
//        redisCache.put("login:"+token,authorities);
        redisTemplate.opsForValue().set("login:"+token,authorities);
        ResultObj resultObj = ResultObj.logingOk();
        resultObj.setData(authorities);

        return resultObj;

//        String pwdMd5  = MD5Utils.stringToMD5(user.getPwd());
//        return loginMapper.getLoginData(user.getUserName(),pwdMd5);
    }

    public ResultList<User> queryAll(User user, Page pageVo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", pageVo.getIndex());
        params.put("size", pageVo.getPageSize());
        int total = loginMapper.queryUserCount();
        List<User> userList= loginMapper.queryAll(user, params);
        ResultList<User> userResultList = new ResultList<User>();
        userResultList.setList(userList);
        userResultList.setTotal(total);
        return userResultList;
    }

    @Override
    public void addUser(User user) {
        String pwdMd5  = MD5Utils.stringToMD5(user.getPwd());
        user.setPwd(pwdMd5);
//        if(loginMapper.countUserName(user)!=1){
//            return false;
//        }
//        if(loginMapper.addUser(user)!=1){
//            return false;
//        }
        loginMapper.addUser(user);
//        return true;
    }

    @Override
    public int changePwd(User user) {
        if (user.getPwd()!=null){
            String pwdMd5  = MD5Utils.stringToMD5(user.getPwd());
            user.setPwd(pwdMd5);
        }
        int result = loginMapper.changePwd(user);
        return result;
    }

    @Override
    public int updateAdmin(User user) {
        int result = loginMapper.updateAdmin(user);
        return result;
    }


}
